package com.haotunet.app.goldtreasure.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.adapter.ProductAdapter;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.GoldInfo;
import com.haotunet.app.goldtreasure.model.ProductItemEntity;
import com.haotunet.app.goldtreasure.model.RecommendProductBean;
import com.haotunet.app.goldtreasure.model.RecommendProductModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.net.UrlUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.DefineListView;
import com.haotunet.app.goldtreasure.views.TitleView;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout.OnRefreshListener;
import java.util.ArrayList;
import java.util.List;

public class GoldFinancialFragment extends BaseFragment {
    private LinearLayout mProductLayout;
    private DefineListView mLvProducct;
    private PullToRefreshLayout mfreshLayout;
    private TitleView mTitleView;
    private TextView mProductTag;
    private boolean isPrepared;
    private boolean mHasLoadedOnce;
    private View view;
    private final int GET_Product_INFO_SUCCESS = 2;
    private final int STATUS_GOLDPRICE_SUCCESS = 3;
    private List<ProductItemEntity> dataList = null;
    private RecommendProductModel model;
    private Handler handle = new Handler() {

        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {

                case GET_Product_INFO_SUCCESS:
                    mProductLayout.setVisibility(View.VISIBLE);
                    model = (RecommendProductModel) msg.obj;
                    List<RecommendProductBean> productList = model.getProductList();
                    dataList = new ArrayList<>();
                    List<RecommendProductBean> activeProductList = model.getActiveProductList();
                    if (activeProductList != null) {
                        if (activeProductList.size() == 0) {
                            mProductTag.setText("活期产品");
                        } else {
                            mProductTag.setText("活动产品");
                        }
                        for (int i = 0; i < activeProductList.size(); i++) {
                            ProductItemEntity actEntity = new ProductItemEntity("活动产品", activeProductList.get(i));
                            dataList.add(actEntity);
                        }
                    }
                    ProductItemEntity currentEntity = new ProductItemEntity("活期产品", productList.get(0));
                    dataList.add(currentEntity);

                    for (int i = 1; i < productList.size(); i++) {
                        ProductItemEntity entity = new ProductItemEntity("定期产品", productList.get(i));
                        dataList.add(entity);
                    }
                    mLvProducct.setAdapter(new ProductAdapter(getActivity(), dataList));
                    break;
                case STATUS_GOLDPRICE_SUCCESS:
                    GoldInfo info = (GoldInfo) msg.obj;
                    Animation loadAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.translate_anim);
                    mTitleView.getPriceView().startAnimation(loadAnimation);
                    mTitleView.getPriceView().setText(info.getCurrentGoldPrice());
                    break;
                default:
                    break;
            }
        }

    };

    public static GoldFinancialFragment newInstance() {
        return new GoldFinancialFragment();
    }

    public GoldFinancialFragment() {
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_gold_financial, container, false);
        initView();
        isPrepared = true;
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        getGoldPrice();
    }

    private void initView() {
        mProductLayout = (LinearLayout) view.findViewById(R.id.product_layout);
        mProductTag = (TextView) view.findViewById(R.id.product_tag);
        mfreshLayout = (PullToRefreshLayout) view.findViewById(R.id.id_swipe_ly);
        mfreshLayout.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
                getProductInfo("refresh");
                getGoldPrice();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {

            }
        });
        mTitleView = (TitleView) view.findViewById(R.id.title_view_main);
        mTitleView.getTitleLayout().setVisibility(View.VISIBLE);
        mTitleView.getGoldTrendView().setVisibility(View.VISIBLE);
        mTitleView.getGoldTrendView().setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ActivityUtils.gotoGPriceTrend(getActivity(), UrlUtil.GOLD_TREND);
            }
        });
        mTitleView.getGuideView().setVisibility(View.VISIBLE);
        mTitleView.getGuideView().setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ActivityUtils.gotoNewGuideLines(getActivity());
            }
        });
        mLvProducct = (DefineListView) view.findViewById(R.id.lv_product);
        mLvProducct.setFocusable(false);
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }
        getProductInfo("");
    }


    private void getProductInfo(final String action) {
        ActionUtil actionUtil = new ActionUtil(getActivity());
        actionUtil.getProductInfo();
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (!isActivityAvailable()) {
                    return;
                }
                RecommendProductModel productModel = (RecommendProductModel) model;
                if (productModel == null) {
                    UIHelper.showToast(getActivity(), getString(R.string.rank_fail));
                    mfreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                    return;
                }
                if (productModel.getErrorMsg() != null) {
                    UIHelper.showToast(getActivity(), productModel.getErrorMsg());

                } else {
                    if (action.equals("refresh")) {
                        mfreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                    mHasLoadedOnce = true;
                    Message msg = new Message();
                    msg.what = GET_Product_INFO_SUCCESS;
                    msg.obj = productModel;
                    handle.sendMessage(msg);
                }
            }

        });
    }

    private void getGoldPrice() {
        ActionUtil actionUtil = new ActionUtil(getActivity());
        actionUtil.getCurrentGoldPrice();
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (!isActivityAvailable()) {
                    return;
                }
                GoldInfo goldInfo = (GoldInfo) model;
                if (goldInfo == null) {
                    UIHelper.showToast(getActivity(), getString(R.string.rank_fail));
                    return;
                }
                if (goldInfo.getErrorMsg() != null) {
                    UIHelper.showToast(getActivity(), goldInfo.getErrorMsg());

                } else {
                    Message msg = new Message();
                    msg.what = STATUS_GOLDPRICE_SUCCESS;
                    msg.obj = goldInfo;
                    handle.sendMessage(msg);
                }
            }
        });
    }
}
